//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
//------------------------------------------------------------------------------
//
//  File:  registry.c
//
#include <windows.h>
#include <ceddk.h>
#include <oal.h>
#include "oal_kitl.h"
#include "monahans_base_regs.h"
#include "Littleton.h"
//------------------------------------------------------------------------------
//
//  Function:  OALKitlInitRegistry
//
//  This function is called as part of IOCTL_HAL_INITREGISTRY to update
//  registry with information about KITL device. This must be done to avoid
//  loading Windows CE driver in case that it is part of image. On image
//  without KITL is this function replaced with empty stub.
//
//
static void UpdateKITLReg(DWORD cfg)
{
    DWORD status;
    HKEY Key;
    DWORD dwDisp;
    status = NKRegCreateKeyEx(HKEY_LOCAL_MACHINE, OAL_KITL_REG_KEY_NAME, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &Key, &dwDisp);
    if (status != ERROR_SUCCESS)
    {
        RETAILMSG(1, (TEXT("Cannot update OAL KITL Reg information.\r\n")));
        return;
    }
    status = NKRegSetValueEx(Key, OAL_KITL_REG_VALUE_NAME_CFG, 0, REG_DWORD, (BYTE *)&cfg, sizeof(cfg));
    if (status != ERROR_SUCCESS)
    {
        RETAILMSG(1, (TEXT("Cannot update OAL KITL Reg information.\r\n")));
    }
    NKRegCloseKey(Key);
}

VOID OALKitlInitRegistry()
{
    DWORD type;
    DWORD PhysicalLoc;
    OAL_KITL_ARGS *pKITLArgs;
    type = OAL_KITL_REG_TYPE_NONE; // default to no type

    OALMSG(OAL_KITL&&OAL_FUNC, (L"+OALKitlInitRegistry\r\n"));
    pKITLArgs = (OAL_KITL_ARGS*) OALArgsQuery(OAL_ARGS_QUERY_KITL);
    if (pKITLArgs == NULL)
    {
        OALMSG(OAL_KITL&&OAL_FUNC, (L"OALKitlInitRegistry -- pKITLArgs is NULL\r\n"));
        goto DoUpdateReg;
    }

    switch (pKITLArgs->devLoc.IfcType)
    {
#ifdef KITL_PCI
    case PCIBus:
        OALPCIRegisterAsUsed(
            pKITLArgs->devLoc.BusNumber, *(OAL_PCI_LOCATION*)&pKITLArgs->devLoc.LogicalLoc
        );
        break;
#endif
    }

    if ((pKITLArgs->flags & OAL_KITL_FLAGS_ENABLED) == 0)
    {
        OALMSG(OAL_KITL&&OAL_FUNC, (L"OALKitlInitRegistry -- KITL is disabled\r\n"));
        goto DoUpdateReg;
    }
    
    PhysicalLoc = (DWORD)pKITLArgs->devLoc.PhysicalLoc;
    if ((PhysicalLoc == LITTLETON_BASE_REG_PA_SMSC_ETHERNET + 0x300)||
        (PhysicalLoc == (DWORD)OALPAtoVA((LITTLETON_BASE_REG_PA_SMSC_ETHERNET + 0x300),FALSE))
       )
    {// currently eth PhysicalLoc is actually VA, which is not PA as USB KITL does
     // we just keep both addresses here make both work for ETH mode.
        OALMSG(OAL_KITL&&OAL_FUNC, (L"OALKitlInitRegistry -- KITL use ETH\r\n"));
        type = OAL_KITL_REG_TYPE_ETH;
    }
    else if (PhysicalLoc == MONAHANS_BASE_REG_PA_USB2H)
    {
        OALMSG(OAL_KITL&&OAL_FUNC, (L"OALKitlInitRegistry -- KITL is USB2\r\n"));
        type = OAL_KITL_REG_TYPE_USB20;
    }
    else if (PhysicalLoc == MONAHANS_BASE_REG_PA_UDC)
    {
        OALMSG(OAL_KITL&&OAL_FUNC, (L"OALKitlInitRegistry -- KITL is USB1\r\n"));
        type = OAL_KITL_REG_TYPE_USB11;
    }
    else
    {
        OALMSG(OAL_KITL&&OAL_FUNC, (L"OALKitlInitRegistry -- KITL default to none, PhysicalLoc=0x%08X \r\n", (DWORD)pKITLArgs->devLoc.PhysicalLoc));
        type = OAL_KITL_REG_TYPE_NONE;
    }

DoUpdateReg:
    UpdateKITLReg(type);
    OALMSG(OAL_KITL&&OAL_FUNC, (L"-OALKitlInitRegistry\r\n"));
}

//------------------------------------------------------------------------------
